﻿using DemoPrism.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoPrism.Services
{
    public class AuthenticationService : IAuthenticationService
    {
        private User _currentUser;
        private bool _isAuthenticated;

        public User CurrentUser
        {
            get => _currentUser;
            private set => SetProperty(ref _currentUser, value);
        }

        public bool IsAuthenticated
        {
            get => _isAuthenticated;
            private set => SetProperty(ref _isAuthenticated, value);
        }

        public event Action<User> UserChanged;

        public async Task<bool> LoginAsync(string username, string password)
        {
            // 模拟登录逻辑
            await Task.Delay(1000);

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                CurrentUser = new User
                {
                    Username = username,
                    Email = $"{username}@example.com",
                    TenantId = "tenant1",
                    IsEmailVerified = false
                };
                IsAuthenticated = true;
                UserChanged?.Invoke(CurrentUser);
                return true;
            }
            return false;
        }

        public async Task LogoutAsync()
        {
            await Task.Delay(500);
            CurrentUser = null;
            IsAuthenticated = false;
            UserChanged?.Invoke(null);
        }

        public async Task<bool> ChangePasswordAsync(string oldPassword, string newPassword)
        {
            // 模拟修改密码逻辑
            await Task.Delay(500);
            return !string.IsNullOrEmpty(oldPassword) && !string.IsNullOrEmpty(newPassword);
        }

        public async Task<bool> SendEmailVerificationAsync()
        {
            // 模拟发送验证邮件
            await Task.Delay(500);
            return true;
        }

        public async Task<bool> VerifyEmailAsync(string code)
        {
            // 模拟验证邮箱
            await Task.Delay(500);
            if (CurrentUser != null && !string.IsNullOrEmpty(code))
            {
                CurrentUser.IsEmailVerified = true;
                UserChanged?.Invoke(CurrentUser);
                return true;
            }
            return false;
        }
    }
}
